﻿package {
	/**
	 * Class Document for FeedBurner Adobe AIR version
	 * Original idea by Napolux.com (http://www.napolux.com/2008/flex-3-adobe-air-feedburner.html)
	 *
	 * @author		Giovambattista Fazioli
	 * @email		g.fazioli@undolog.com
	 * @web			http://www.undolog.com
	 *
	 * CHANGE LOG
	 *
	 * ver 0.1
	 *	- First Adobe AIR release
	 *
	 */	
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
		
	public class FeedBurner extends MovieClip {

		private const FEEDBURNER_API		:String		= 'http://api.feedburner.com/awareness/1.0/GetFeedData';
		
		public function FeedBurner():void {
			trace("construtor::FeedBurner");
			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		//

		private function init( e:Event = null ):void {
			result_txt.text = '';
			feed_url.text 	= 'http://www.undolog.com/feed';
			//
			check_btn.addEventListener( MouseEvent.CLICK, checkFeedBurnerReaders );
		}
		
		//
		private function checkFeedBurnerReaders(e:MouseEvent = null ):void {
			trace( "FeedBurner::checkFeedBurnerReaders()" );
			//
			var loader		:URLLoader 		= new URLLoader();
			var urlreq		:URLRequest 	= new URLRequest();
			var param		:URLVariables	= new URLVariables();
			//
			param.uri						= feed_url.text;
			urlreq.url 						= FEEDBURNER_API;
			urlreq.method 					= URLRequestMethod.GET;
			urlreq.data						= param;
			//	
			loader.dataFormat 				= URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, completeHandler);
			try {
				loader.load(urlreq);
			} catch (error:Error) {
				trace("Errore nel caricamento dell' URL");
			}
			function completeHandler(event:Event):void {
				//<rsp stat="ok">
				//  <feed id="651641" uri="undolog">
				//    <entry date="2008-11-17" circulation="389" hits="1348" downloads="0" reach="43"/>
				//  </feed>
				//</rsp>
				var x_xml	:XML 			= XML(event.target.data);
				if( x_xml.@stat == 'ok' ) result_txt.text = x_xml.feed.entry.@circulation.toString();
				else result_txt.text = '0';
			}
		}
	}
}